const router = require('koa-router')()
const { login } = require('../../../../common/controller/user')
const {
	ErrorModel,
	SuccessModel
} = require('../../../../common/model/res-model')

router.prefix('/api/user')

router.post('/login', async (ctx, next) => {
	const { username, password } = ctx.request.body
	const userData = await login(username, password)

	if (userData.username) {
		ctx.session.username = userData.username
		ctx.session.realname = userData.realname

		return (ctx.body = new SuccessModel())
	}
	return (ctx.body = new ErrorModel('登录失败'))
})

// router.get('/session-test', async (ctx, next) => {
// 	if (!ctx.session.viewCount) {
// 		ctx.session.viewCount = 0
// 	}
// 	ctx.session.viewCount++

// 	ctx.body = {
// 		errno: 0,
// 		viewCount: ctx.session.viewCount
// 	}
// })

module.exports = router
